package cn.kailangtu.dataspot.activify;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import cn.kailangtu.commonevents.eventEntity.KeyboardLockerConfirmActionEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardLockerConfirmResultEvent;
import cn.kailangtu.commontools.APPSPUtils;
import cn.kailangtu.commontools.PasswordUtils;
import cn.kailangtu.commontools.ToastUtils;
import cn.kailangtu.dataspot.R;
import cn.kailangtu.dataspot.data.RandomPwd;
import cn.kailangtu.dataspot.data.consts.KeyboardAction;
import cn.kailangtu.dataspot.data.context.KeyboardLockerConfirmContext;
import cn.kailangtu.dataspot.databinding.FragmentKeyboardLockerComfirmBinding;
import cn.kailangtu.dataspot.status;

public class KeyboardLockerConfirmFragment extends KeyboardBaseFragment {

    private FragmentKeyboardLockerComfirmBinding binding;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentKeyboardLockerComfirmBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListener();

        binding.tvPwdTip.setText(""+ RandomPwd.random);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    private void initListener(){
        binding.tvConfirmPwd1.setOnClickListener(keybaodListener);
        binding.tvConfirmPwd2.setOnClickListener(keybaodListener);
        binding.tvConfirmPwd3.setOnClickListener(keybaodListener);
        binding.tvConfirmPwd4.setOnClickListener(keybaodListener);
        binding.btnConfirmCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reset();
                EventBus.getDefault().post(new KeyboardLockerConfirmResultEvent(false));
            }
        });
        binding.btnConfirmSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String pwd = binding.tvConfirmPwd1.getText().toString()
                        + binding.tvConfirmPwd2.getText().toString()
                        + binding.tvConfirmPwd3.getText().toString()
                        + binding.tvConfirmPwd4.getText().toString();
                String str = PasswordUtils.toPwd(RandomPwd.random);
                boolean result = false;
                if(pwd.equals(str))
                {
                    result = true;
                }
                else {
                    if(status.currentPwdTargetSourceId == R.layout.fragment_common_setting)
                    {
                        if(pwd.equals(APPSPUtils.getInstance().getCommonSettingPwd()))
                        {
                            result = true;
                        }
                    }
                    else if(status.currentPwdTargetSourceId == R.layout.fragment_factory_setting){
                        Log.d("DDDDDDDDDDDDD",pwd+"|"+APPSPUtils.getInstance().getFactorySettingPwd());
                        if(pwd.equals(APPSPUtils.getInstance().getFactorySettingPwd()))
                        {
                            result = true;
                        }
                    }
                    else if(status.currentPwdTargetSourceId  == R.layout.fragment_common_setting_low){
                        if(pwd.equals(APPSPUtils.getInstance().getCommonSettingPwdLow()))
                        {
                            result = true;
                        }
                    }
                    else if(status.currentPwdTargetSourceId == R.layout.fragment_factory_setting_low){
                        if(pwd.equals(APPSPUtils.getInstance().getFactorySettingPwdLow()))
                        {
                            result = true;
                        }
                    }
                }
                if(result){
                    reset();
                    //ToastUtils.ShowInCenter(getContext(),"密码正常");
                    Log.e("DDDDDDDDDDDDD","dddd");
                    EventBus.getDefault().post(new KeyboardLockerConfirmResultEvent(true));
                }
                else
                {
                    ToastUtils.ShowInCenter(getContext(),"密码错误");
                }
                reset();
            }
        });
    }

    private View.OnClickListener keybaodListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView current = (TextView)view;
            resetText(current);

        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(KeyboardLockerConfirmActionEvent event){
        keyboard(event.getActionValue());
    }


    private void keyboard(int action ) {
        switch (action) {
            case KeyboardAction.OPEN:
                reset();
                break;
            case KeyboardAction.ENTRY:
                if (KeyboardLockerConfirmContext.getSelectedView().equals(binding.btnConfirmSave)) {
                    binding.btnConfirmSave.callOnClick();
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.btnConfirmCancel))
                {
                    binding.btnConfirmCancel.callOnClick();
                }
                else {
                    resetText(KeyboardLockerConfirmContext.getSelectedView());
                }

                break;
            case KeyboardAction.RIGHT:
                if(KeyboardLockerConfirmContext.getSelectedView() == null)
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd1);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.tvConfirmPwd1))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd2);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.tvConfirmPwd2))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd3);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.tvConfirmPwd3))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd4);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.tvConfirmPwd4))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.btnConfirmCancel);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.btnConfirmCancel))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.btnConfirmSave);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.btnConfirmSave))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd1);
                }
                else
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd1);
                }
                break;
            case KeyboardAction.LEFT:
                if(KeyboardLockerConfirmContext.getSelectedView() == null)
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd4);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.tvConfirmPwd4))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd3);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.tvConfirmPwd3))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd2);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.tvConfirmPwd2))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd1);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.tvConfirmPwd1))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.btnConfirmSave);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.btnConfirmSave))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.btnConfirmCancel);
                }
                else if(KeyboardLockerConfirmContext.getSelectedView().equals(binding.btnConfirmCancel))
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd4);
                }
                else
                {
                    KeyboardLockerConfirmContext.setSelectedView(binding.tvConfirmPwd1);
                }
                break;
        }
    }

    private void resetText(TextView textView){
        int oldValue = Integer.valueOf(textView.getText().toString());
        oldValue++;
        if(oldValue>9)
        {
            oldValue = 0;
        }
        textView.setText(String.valueOf(oldValue));
    }

    public void reset()
    {
        binding.tvConfirmPwd1.setText("0");
        binding.tvConfirmPwd2.setText("0");
        binding.tvConfirmPwd3.setText("0");
        binding.tvConfirmPwd4.setText("0");

        binding.tvPwdTip.setText(""+RandomPwd.random);
    }
}
